package ru.zhigulin.io;

import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricModelProvider;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricRecipeProvider;
import net.minecraft.data.client.BlockStateModelGenerator;
import net.minecraft.data.client.ItemModelGenerator;
import net.minecraft.data.client.Models;
import net.minecraft.data.server.recipe.CookingRecipeJsonBuilder;
import net.minecraft.data.server.recipe.RecipeExporter;
import net.minecraft.data.server.recipe.ShapelessRecipeJsonBuilder;
import net.minecraft.item.Items;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.book.RecipeCategory;
import net.minecraft.registry.RegistryWrapper;
import ru.zhigulin.io.group.ModGroups;
import ru.zhigulin.io.item.ModItems;

import java.util.concurrent.CompletableFuture;

public class ShootAndFunDataGenerator implements DataGeneratorEntrypoint {
	@Override
	public void onInitializeDataGenerator(FabricDataGenerator fabricDataGenerator) {
		var pack = fabricDataGenerator.createPack();
		pack.addProvider(SAFRecipeGenerator::new);
		pack.addProvider(SAFModelGenerator::new);
		pack.addProvider(SAFLanguageGeneratorEnUs::new);
	}

	private static class SAFModelGenerator extends FabricModelProvider {

		public SAFModelGenerator(FabricDataOutput output) {
			super(output);
		}

		@Override
		public void generateBlockStateModels(BlockStateModelGenerator blockStateModelGenerator) {

		}

		@Override
		public void generateItemModels(ItemModelGenerator itemModelGenerator) {
			itemModelGenerator.register(ModItems.RAW_MARSHMALLOW, Models.GENERATED);
			itemModelGenerator.register(ModItems.MARSHMALLOW_ON_STICK, Models.GENERATED);
			itemModelGenerator.register(ModItems.COOKED_MARSHMALLOW_ON_STICK, Models.GENERATED);
		}
	}

	private static class SAFRecipeGenerator extends FabricRecipeProvider {

		public SAFRecipeGenerator(
				FabricDataOutput output,
				CompletableFuture<RegistryWrapper.WrapperLookup> registriesFuture
		) {
			super(output, registriesFuture);
		}

		@Override
		public void generate(RecipeExporter exporter) {
			ShapelessRecipeJsonBuilder
					.create(RecipeCategory.FOOD, ModItems.RAW_MARSHMALLOW, 8)
					.input(Items.APPLE)
					.input(Items.EGG)
					.input(Items.SUGAR)
					.criterion(hasItem(Items.APPLE), conditionsFromItem(Items.APPLE))
					.criterion(hasItem(Items.EGG), conditionsFromItem(Items.EGG))
					.criterion(hasItem(Items.SUGAR), conditionsFromItem(Items.SUGAR))
					.offerTo(exporter);

			ShapelessRecipeJsonBuilder
					.create(RecipeCategory.FOOD, ModItems.MARSHMALLOW_ON_STICK)
					.input(Items.STICK)
					.input(ModItems.RAW_MARSHMALLOW, 2)
					.criterion(hasItem(ModItems.RAW_MARSHMALLOW), conditionsFromItem(ModItems.RAW_MARSHMALLOW))
					.offerTo(exporter);

			CookingRecipeJsonBuilder
					.createCampfireCooking(
							Ingredient.ofItems(ModItems.MARSHMALLOW_ON_STICK),
							RecipeCategory.FOOD,
							ModItems.COOKED_MARSHMALLOW_ON_STICK,
							1, // exp
							5 // time
					).criterion(
							hasItem(ModItems.MARSHMALLOW_ON_STICK),
							conditionsFromItem(ModItems.MARSHMALLOW_ON_STICK)
					).offerTo(exporter);
		}
	}

	private static class SAFLanguageGeneratorEnUs extends FabricLanguageProvider {
		public SAFLanguageGeneratorEnUs(FabricDataOutput dataOutput, CompletableFuture<RegistryWrapper.WrapperLookup> registryLookup) {
			super(dataOutput, registryLookup);
		}

		@Override
		public void generateTranslations(RegistryWrapper.WrapperLookup registryLookup, TranslationBuilder translationBuilder) {
			translationBuilder.add(ModItems.RAW_MARSHMALLOW, "Raw Marshmallow");
			translationBuilder.add(ModItems.MARSHMALLOW_ON_STICK, "Marshmallow on Stick");
			translationBuilder.add(ModItems.COOKED_MARSHMALLOW_ON_STICK, "Cooked Marshmallow on Stick");
			translationBuilder.add(ModGroups.FOOD_ITEM_GROUP_KEY, "SAF Food");
		}
	}
}
